﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MLMBonus.Engine;

namespace MLMBonus.Unilevel
{
    public class Order : IVolume
    {
        #region IVolume Members

        public string Id { get; set; }

        public bool Pay(string BonusRunType, INode Distributor)
        {
            Distributor dist = Distributor as Distributor;

            if (dist.IsRetail)
                return false;

            if (PVDistributor.Equals(dist))
            {
                if (RetailAmt.HasValue)
                {
                    dist.Context.Pay(this, dist, "Retail Bonus", 0, RetailAmt.Value, null);
                }

                CurrentLevel = 0;
                CurrentGeneration = 0;
            }

            if (dist.IsActive)
            {
                decimal payPer;
                decimal payAmt;
                decimal? payFast;
                if (dist.qualifiedUnilevels <= CurrentLevel)
                {
                    payPer = dist.uniContext.getUnilevelPercent(CurrentLevel);
                    payAmt = this.payVol * payPer;
                    dist.Context.Pay(this, dist, "Unilevel", CurrentLevel, payAmt, payPer);
                    if (Properties.Settings.Default.FullCompression)
                    {
                        CurrentLevel++;
                    }
                }

                if (dist.qualifiedGenerations <= CurrentGeneration && dist.isQualifiedBreakaway)
                {
                    payPer = dist.uniContext.getGenerationPercent(CurrentGeneration);
                    payAmt = this.payVol * payPer;
                    dist.Context.Pay(this, dist, "Generation Payout", CurrentGeneration, payAmt, payPer);
                }

                if(IsFastStart && !FastStartPaid)
                {
                    payFast = dist.uniContext.getFastStartPercent(dist.CurrentRankId);
                    if (payFast.HasValue)
                    {
                        if (FastStartAmount > 0)
                        {
                            dist.Context.Pay(this, dist, "Fast Start Payout", 1, FastStartAmount.Value, null);
                        }
                        else
                        {
                            FastStartAmount = this.payVol * payFast.Value;
                            dist.Context.Pay(this, dist, "Fast Start Payout", 1, FastStartAmount.Value, null);
                        }
                        FastStartPaid = true;
                    }
                }

                if (!Properties.Settings.Default.FullCompression)
                {
                    CurrentLevel++;
                }
            }

            if (dist.IsBreakaway)
                CurrentGeneration++;

            if (PVDistributor.Equals(dist) && CurrentLevel == 0)
                CurrentLevel = 1;

            return (CurrentLevel > UnilevelContext.maxLevels && CurrentGeneration > UnilevelContext.maxGens && (!IsFastStart || FastStartPaid));
        }

        #endregion

        #region Volume properties
        internal Distributor PVDistributor { get; set; }
        internal Distributor OriginatingDistributor { get; set; }
        public int CurrentLevel { get; set; }
        public int CurrentGeneration { get; set; }

        public decimal PV { get; set; }
        public decimal? CV { get; set; }
        private decimal payVol { get { return this.CV ?? this.PV; } }
        public decimal? RetailAmt { get; set; }
        public decimal? FastStartAmount { get; set; }
        public bool IsFastStart { get; set; }
        internal bool FastStartPaid { get; set; }
        #endregion
    }
}
